package ui;

import entity.FeedBackInfo;
import entity.RuleInfo;
import entity.TableName;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;

import java.io.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class QueryValidateFun {
    private DataTable dt;
    private String queryCode;
    private String queryName;

    public QueryValidateFun(DataTable dt, String queryCode, String queryName){
        this.dt = dt;
        this.queryCode = queryCode;
        this.queryName = queryName;
    }

    public void paddingTableData(DataTable dt,String queryCode, String queryName) throws Exception {
        System.out.println("校验查询编码为：" + queryCode + "，校验查询名称为:" + queryName);

        InputStream codeInputStream = this.getClass().getResourceAsStream("/file/人行反馈信息代码.csv");
        InputStreamReader isr = new InputStreamReader(codeInputStream, "gb2312");

        InputStream validInputStream = this.getClass().getResourceAsStream("/file/人行校验.csv");
        InputStreamReader validsr = new InputStreamReader(validInputStream, "gb2312");

        BufferedReader br = new BufferedReader(isr);
        BufferedReader validbr = new BufferedReader(validsr);

        String line = null,validLine = null;

        Vector<Vector<String>> tableData = this.dt.getTableData();
        tableData.clear();

        Map<String,RuleInfo> ruleInfoMap = new HashMap<>();
        while ((validLine = validbr.readLine()) != null) {
            String[] lineUnit = validLine.split("[,]{1,}",3);
            RuleInfo ruleInfo = new RuleInfo();
            ruleInfo.setRuleCode(lineUnit[0]);
            ruleInfo.setRuleComments(lineUnit[1]);
            ruleInfoMap.put(lineUnit[0],ruleInfo);
        }

        List<FeedBackInfo> feedBackInfoList = new ArrayList<>();
        while ((line = br.readLine()) != null) {
            String[] lineUnit = line.split("[,]{1,}",5);
            FeedBackInfo feedBackInfo = new FeedBackInfo();
            feedBackInfo.setFeedBackMessageCode(lineUnit[0]);
            feedBackInfo.setFeedBackTitle(lineUnit[1]);
            feedBackInfo.setRuleCode(lineUnit[2]);
            feedBackInfo.setFeedBackComments(lineUnit[3]);
            RuleInfo ruleInfo = ruleInfoMap.get(feedBackInfo.getRuleCode());
            if(ruleInfo != null){
                feedBackInfo.setRuleComments(ruleInfo.getRuleComments());
            }else{
                feedBackInfo.setRuleComments("");
            }
            feedBackInfoList.add(feedBackInfo);
        }

        List<FeedBackInfo> firstResultList = new ArrayList<>();
        if(StringUtils.isNotBlank(queryCode)){
            Pattern pattern1 = null;
            for(int i=0;i<feedBackInfoList.size();i++){
                pattern1 = Pattern.compile(queryCode);
                Matcher matcher1 = pattern1.matcher(feedBackInfoList.get(i).getFeedBackMessageCode());
                if(matcher1.find()){
                    firstResultList.add(feedBackInfoList.get(i));
                }
            }
        }

        List<FeedBackInfo> sencondQueryList = new ArrayList<>();
        if(firstResultList.size() != 0){
            CollectionUtils.addAll(sencondQueryList,new Object[firstResultList.size()]);
            Collections.copy(sencondQueryList, firstResultList);
        }else if(StringUtils.isBlank(queryCode)){
            /**
             * 如果查询框为空情况下查询所有内容
             */
            CollectionUtils.addAll(sencondQueryList,new Object[feedBackInfoList.size()]);
            Collections.copy(sencondQueryList, feedBackInfoList);
        }

        List<FeedBackInfo> finalResultList = new ArrayList<>();
        if(StringUtils.isNotBlank(queryName)){
            Pattern pattern2 = null;
            for(int j=0;j<sencondQueryList.size();j++){
                pattern2 = Pattern.compile(queryName);
                Matcher matcher2 = pattern2.matcher((sencondQueryList.get(j)).getFeedBackTitle());
                if(matcher2.find()){
                    finalResultList.add(sencondQueryList.get(j));
                }
            }
        }else{
            CollectionUtils.addAll(finalResultList,new Object[sencondQueryList.size()]);
            Collections.copy(finalResultList, sencondQueryList);
        }

        if(finalResultList.size() > 0){
            for(int k=0;k<finalResultList.size();k++){
                FeedBackInfo feedBackInfo = finalResultList.get(k);

                Vector<String> vecUnit = new Vector<>();
                vecUnit.add(feedBackInfo.getFeedBackMessageCode());
                vecUnit.add(feedBackInfo.getFeedBackTitle());
                vecUnit.add(feedBackInfo.getFeedBackComments());
                vecUnit.add(feedBackInfo.getRuleCode());
                vecUnit.add(feedBackInfo.getRuleComments());
                tableData.add(k,vecUnit);
                this.dt.updateUI();
            }
        }else{
            throw new RuntimeException("找不到查询内容!");
        }
    }
}
